package com.sinosoft.sinopay.web.batch.biz.service.impl;


import com.sinosoft.sinocloud.common.biz.service.ReportLogService;
import com.sinosoft.sinocloud.platform.common.exception.biz.IllegalRequestException;
import com.sinosoft.sinocloud.platform.common.exception.checked.RepeatRequestException;
import com.sinosoft.sinocloud.platform.common.util.SecurityUtil;
import com.sinosoft.sinopay.merchants.facade.MerchantInfoFacadeService;
import org.springframework.beans.factory.annotation.Autowired;

import com.sinosoft.sinopay.web.batch.biz.service.PaymentBatchService;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 【 数据验证类实现 】
 *
 * @author chenzhiheng
 * @version V1.0
 * @date 16/12/20
 */
@Service
public class PaymentBatchServiceImpl implements PaymentBatchService {

    @Autowired
    MerchantInfoFacadeService merchantInfoFacadeService;

    @Autowired
    private ReportLogService reportLogService;

    /** 商户签名信息缓存map */
    private  Map<String,String> signKeysMap;

    /**
     * 获取所有商户签名信息
     *@return
     */
    @Override
    public void getAllMerchantSignKeys(String sign,String requestXml,String merchantCode){
        if(signKeysMap ==null || signKeysMap.size() == 0)
            signKeysMap = merchantInfoFacadeService.getAllMerchantSignKeys();

        String tSignKey = signKeysMap.get(merchantCode);
        String tCompareSignMsg = SecurityUtil.Md5(requestXml+tSignKey);
        if(sign ==null || !sign.equals(tCompareSignMsg)){
            throw new IllegalRequestException("签名不合法");
        }
    }


    @Override
    public void checkRepeatRequest(String merchantCode, String serialNo) throws RepeatRequestException {
        if(serialNo==null || serialNo==""){
            throw new RepeatRequestException("交易流水号不能为空");
        }
        reportLogService.checkRepeatRequest(merchantCode,serialNo);
    }
}
